<?php
require('config.inc.php');
require('mysql.inc.php');
//require('header.php');

function dateform(){
	$month = "";
	$monthform = "";
	
	$thismonth = date("M");
	$thisyear = date("Y");
	$thisday = date("i");
	
	for($i=1;$i<=12;$i++){
	// Create our month form.
		$month = date("M",mktime(0,0,0,$i,1,date("Y")));
		if($month == date("M")){
			$monthform .= "<option value='$i' SELECTED>".$month."</option>\n";
		}else{
			$monthform .= "<option value='$i'>".$month."</option>\n";
		}
	} $monthform = "<select name='month'>\n".$monthform."</select>\n";
	
	$year = "";
	$yearform = "";
	for($year=date("Y",mktime(0,0,0,1,1,date("Y")-1));$year<=date("Y",mktime(0,0,0,1,1,date("Y")+2));$year++){
		if($year == date("Y")){
			$yearform .= "<option SELECTED>".$year."</option>\n";
		}else{
			$yearform .= "<option>".$year."</option>\n";
		}
	}$yearform = "<select name='year'>\n".$yearform."</select>\n";
	
	$day = "";
	$dayform = "";
	for($day=1;$day<=31;$day++){
		if($day == date("j")){
			$dayform .= "<option SELECTED>".$day."</option>\n";
		}else{
			$dayform .= "<option>".$day."</option>\n";
		}
	}$dayform = "<select name='day'>\n".$dayform."</select>\n";

	$form = $monthform.$dayform.$yearform;
	return $form;
}

function listicons(){
	if($handle = opendir("./resources/icons")){
		while (false !== ($file = readdir($handle))) {
			if((substr($file,0,1)!=".")){
        		//$icons .= "<li style='list-style:none'><input type=radio name=icon value='$file'><img src='./resources/icons/$file'></li>";
        		
$icons .= "<label><input name=\"icon\" value=\"".$file."\" type=\"radio\"><img src=\"./resources/icons/".$file."\"></label>";
			}
    	}
	}
	return $icons;
}

if(!isset($_GET['key']) && !isset($_POST['new'])){
	include('header.php');
	?>
		<p>Make your own 'quit smoking' banner and actively track your progress every day!</p>
		
		<label> Email (Optional since we really don't need it) <input name="email" id="email" type="text"> </label>
		<label>Your first full day of not smoking! 
			<span>
			<input type=hidden name=new value=true>
			<?=dateform();?> 
			</form>
			</span>
		</label>
		
		<label>How many cigarettes a day?<input name="smokedperday" id="smokedperday"type="text"> </label>
		<label>The cost per pack.$<input name="costperpack" id="costperpack" type="text"> </label>
		
		<p>Now choose an icon for your banner<p>
		
		<div class="radioGroup">
			<?=listicons();?>
		</div>
		<label><input value="Create Banner" type="submit" onclick="document.forms.createbanner.submit()"></label>
	<?
	include('footer.php');
}elseif(isset($_POST['new'])){
	include('header.php');
	require('new.php');
			echo "
			<img src='./?key=$key'> <br />
			Link: <a href='./?key=$key'>Link to Image</a><br />
			<br />
			BBcode:<br />
			<textarea cols=50 rows=3>[url=\"".$url."\"]\n[img]".$url."?key=".$key."[/img]\n[/url]</textarea><br />
			<br />
			HTML:<br />
			<textarea cols=50 rows=3><a href=\"".$url."\">\n<img border=\"0\" src=\"".$url."?key=".$key."\">\n</a></textarea><br />
			<br />
			<a href='./'>Make another Smoke Free banner</a> <br />
			";
	include('footer.php');
}else{
	require('showimage.php');
}